/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */
package es.silenus.detecta.controller;

import es.silenus.detecta.Constants;

import es.silenus.detecta.model.User;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.context.MessageSource;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Base controller.
 *
 * @author <a href="mailto:malonso@silenus-consultoria.es">Mariano Alonso</a>
 *
 * @since 28-mar-2009 17:20:50
 */
public class BaseController extends MultiActionController {
	/**
	 * Logger for this class.
	 */
	private static final Log LOG = LogFactory.getLog(BaseController.class);

	/**
	 * Default view name for simple success results.
	 */
	public static final String JSON_SUCCESS_VIEW = "success";

	/**
	 * The message source.
	 */
	protected MessageSource messageSource;

	/**
	 * Sets the message source.
	 *
	 * @param messageSource the message source.
	 */
	public void setMessageSource(final MessageSource messageSource) {
		this.messageSource = messageSource;
	}

	/**
	 * Retrieves the message source.
	 *
	 * @return the message source.
	 */
	public MessageSource getMessageSource() {
		return messageSource;
	}

	/**
	 * Retrieves the current user.
	 *
	 * @param request the request.
	 *
	 * @return the request.
	 */
	public User getUser(final HttpServletRequest request) {
		return (User)request.getUserPrincipal();
	}

	/**
	 * Prints a text to the response, with a given content-type.
	 *
	 * @param response the response.
	 * @param contentType the content type.
	 * @param text the text to print.
	 */
	public void print(final HttpServletResponse response, final String contentType, final String text) {
		try {
			response.setCharacterEncoding(Constants.ENCODING);
			response.setContentType(contentType);

			PrintWriter pw = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), Constants.ENCODING));

			pw.print(text);
			pw.flush();
		} catch(IOException e) {
			LOG.error("IO error writing response", e);
		}
	}

	/**
	 * Prints a json content to the response.
	 *
	 * @param response the response.
	 * @param text the json text.
	 */
	public void printJSON(final HttpServletResponse response, final String text) {
		print(response, Constants.JSON_CONTENT, text);
	}

	/**
	 * Prints a plain content to the response.
	 *
	 * @param response the response.
	 * @param text the plain text.
	 */
	public void printPlain(final HttpServletResponse response, final String text) {
		print(response, Constants.PLAIN_CONTENT, text);
	}

	/**
	 * Prints a plain content to the response.
	 *
	 * @param response the response.
	 * @param text the plain text.
	 */
	public void printHTML(final HttpServletResponse response, final String text) {
		print(response, Constants.HTML_CONTENT, text);
	}

	/**
	 * Prints a plain content to the response.
	 *
	 * @param response the response.
	 * @param text the plain text.
	 */
	public void printXML(final HttpServletResponse response, final String text) {
		print(response, Constants.XML_CONTENT, text);
	}
}
